/*
 * Created: 2022-08-31 19:58:47
 * Author : 涛涛涛
 */

import { myEnv } from '@/myEnv';
import { mergeRequest } from '@/utils';
import { Product_Res, Product_Web } from '@/pages/_server/_class/Product/Product';
import { apiGetArtworkById } from './common/apiGetArtworkById';
import { apiGetAuthorById } from './common/apiGetAuthorById';
type ApiParams = {
    productId: string;
    artwork?: boolean;
    artist?: boolean;
};
/** 通过ID获得商品信息 */
export const apiGetProductById = async (params: ApiParams) => {
    const { productId, artist = true, artwork = true } = params;
    const productRes: MyResponse<Product_Res> = await sendRequest(productId);
    const product = new Product_Web().immit_response(productRes.data);
    let promiseArr = [];
    if (artwork === true) {
        let p1 = apiGetArtworkById(product.artworkId).then((artRes) => product.immit_artwork(artRes.data));
        promiseArr.push(p1);
    }
    if (artist === true) {
        let p2 = apiGetAuthorById(product.artistId).then((authRes) => product.immit_author(authRes.data));
        promiseArr.push(p2);
    }
    await Promise.all(promiseArr);
    const resonlse: MyResponse<Product_Web> = { ...productRes, data: product };
    return resonlse;
};

const sendRequest = async (productId: string) => {
    let response1: MyResponse<Product_Res>;
    if (myEnv.IS_ON_LINE) {
        response1 = await mergeRequest({
            url: `goods?id=${productId}`,
            method: 'GET',
        });
    } else {
        response1 = {
            code: '200',
            message: 'ok',
            data: {
                id: '1',
                artworkId: '1',
                artistId: '1',
                dropStartDate: '2022-10-02T06:00:00.000Z',
                dropEndDate: '2022-10-02T06:30:00.000Z',
                totalQuantity: '1000',
                leftQuantity: '500',
                price: '30.05',
                status: 'waitForSale',
            },
        };
    }
    return response1;
};
